package com.ruoyi.office.asset.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AffectedRows;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PagingData;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanCopier;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.office.asset.domain.AssetLocation;
import com.ruoyi.office.asset.request.AssetLocationCreateDto;
import com.ruoyi.office.asset.request.AssetLocationQueryDto;
import com.ruoyi.office.asset.request.AssetLocationUpdateDto;
import com.ruoyi.office.asset.response.AssetLocationVo;
import com.ruoyi.office.asset.service.IAssetLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 资产位置
 *
 * @author tqfeiyang
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/office/asset/assetLocation")
@RequiredArgsConstructor
@Api("资产位置管理")
public class AssetLocationController extends BaseController
{
    private final IAssetLocationService assetLocationService;

    /**
     * 分页查询资产位置
     */
    @GetMapping("/query")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:query')")
    @ApiOperation("分页查询资产位置")
    public R<PagingData<AssetLocationVo>> queryAssetLocationPage(@Validated AssetLocationQueryDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<AssetLocation> list = assetLocationService.selectAssetLocationList(dto);
        return R.ok(PagingData.of(list, AssetLocationVo.class));
    }

    /**
     * 导出资产位置
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:export')")
    public void exportAssetLocationList(HttpServletResponse response, @Validated AssetLocationQueryDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<AssetLocation> list = assetLocationService.selectAssetLocationList(dto);
        new ExcelUtil<>(AssetLocation.class).exportExcel(response, list, "资产位置");
    }

    /**
     * 检索所有资产位置
     */
    @GetMapping("/retrieve")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:retrieve')")
    public R<List<AssetLocation>> retrieveAssetLocationList()
    {
        return R.ok(assetLocationService.selectAssetLocationList(new AssetLocationQueryDto()));
    }

    /**
     * 获取资产位置详细信息
     */
    @GetMapping("/obtain")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:obtain')")
    public R<AssetLocationVo> obtainAssetLocation(@RequestParam Integer id)
    {
        AssetLocation assetLocation = assetLocationService.selectAssetLocationById(id);
        return R.ok(BeanCopier.copy(assetLocation, AssetLocationVo.class));
    }

    /**
     * 新增资产位置
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:create')")
    @Log(title = "资产位置", businessType = BusinessType.INSERT)
    public R<AffectedRows> createAssetLocation(@Validated @RequestBody AssetLocationCreateDto dto)
    {
        AssetLocation assetLocation = BeanCopier.copy(dto, AssetLocation.class);
        assetLocation.setCreateAt(LocalDateTime.now());
        assetLocation.setCreateBy(SecurityUtils.getUsername());
        assetLocation.setUpdateAt(LocalDateTime.now());
        assetLocation.setUpdateBy(SecurityUtils.getUsername());
        return R.rows(assetLocationService.insertAssetLocation(assetLocation));
    }

    /**
     * 修改资产位置
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:update')")
    @Log(title = "资产位置", businessType = BusinessType.UPDATE)
    public R<AffectedRows> updateAssetLocation(@Validated @RequestBody AssetLocationUpdateDto dto)
    {
        AssetLocation assetLocation = BeanCopier.copy(dto, AssetLocation.class);
        assetLocation.setUpdateAt(LocalDateTime.now());
        assetLocation.setUpdateBy(SecurityUtils.getUsername());
        return R.rows(assetLocationService.updateAssetLocation(assetLocation));
    }

    /**
     * 删除资产位置
     */
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:delete')")
    @Log(title = "资产位置", businessType = BusinessType.DELETE)
    public R<AffectedRows> deleteAssetLocationById(@RequestParam Integer id)
    {
        return R.rows(assetLocationService.deleteAssetLocationById(id));
    }

    /**
     * 批量删除资产位置
     */
    @PostMapping("/batchDelete")
    @PreAuthorize("@ss.hasPermi('office:asset:assetLocation:batchDelete')")
    @Log(title = "资产位置", businessType = BusinessType.DELETE)
    public R<AffectedRows> batchDeleteAssetLocationList(@RequestBody Integer[] ids)
    {
        return R.rows(assetLocationService.deleteAssetLocationByIds(ids));
    }
}
